package cn.col.gszf.service.impl;

import cn.col.gszf.mapper.UserVotesMapper;
import cn.col.gszf.model.UserVoteGroupDay;
import cn.col.gszf.pojo.UserVotes;
import cn.col.gszf.service.UserVotesService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;

/**
 * @author wanghong
 * @created 2024-06-19 14:56
 * @description 用户投票实现类
 **/
@Service
public class UserVotesServiceImpl implements UserVotesService {

    @Resource
    private UserVotesMapper uservotesMapper;

    /**
    * @Description 查询用户投票信息
    * @Param: ip，vid，channelId
    * @return: Boolean
    * @Author: wanghong
    * @Date: 2024/6/23
    */
    @Override
    public boolean checkUser(String ip, String vid, Integer channelId) {
        QueryWrapper<UserVotes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vid", vid).eq("channel_id", channelId);
        Long count = uservotesMapper.selectCount(queryWrapper);
        if (count == 0) {
            return false;
        }

        QueryWrapper<UserVotes> wrapper = queryWrapper.eq("user_ip", ip).eq("channel_id", channelId);
        Long selectCount = uservotesMapper.selectCount(wrapper);
        return selectCount <= 10;
    }

    /**
    * @Description 根据vid和channelId查询用户投票信息
    * @Param: vid，channelId
    * @return: List<UserVotes>
    * @Author: wanghong
    * @Date: 2024/6/23
    */
    @Override
    public List<UserVotes> getByVIDAndChannelId(String vid, Integer channelId) {
        Wrapper<UserVotes> wrapper = new QueryWrapper<UserVotes>().eq("vid", vid).eq("channel_id", channelId);
        return uservotesMapper.selectList(wrapper);
    }


    @Override
    public List<UserVotes> findAllUserVotes(Page<UserVotes> rowPage) {
        IPage<UserVotes> u = rowPage;
        return u.getRecords();
    }

    @Override
    public List<UserVoteGroupDay> calByDay() {
        return uservotesMapper.findNumsGroupByDay();
    }

    @Override
    public List<UserVoteGroupDay> calByDayAndChannelId(Integer channelId) {
        return uservotesMapper.findNumsGroupByDayAndChannelId(channelId);
    }

    @Override
    public HashMap<String, Integer> getTodayResult() {
        HashMap<String, Integer> mp = new HashMap<>();
        LocalDateTime start =  LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end =  LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        //总投票数
        Integer votingNum = uservotesMapper.getSumVotingNums();
        //今日单频道投票数
        Integer maxVotingNums = uservotesMapper.getMaxVotingNums(start, end);
        //今日投票数
        Integer totalVotingNum = uservotesMapper.countByCreateAtBetween(start, end);

        mp.put("voting_num", null == votingNum ? 0 :votingNum);
        mp.put("max_voting_num", null == maxVotingNums ? 0 :maxVotingNums);
        mp.put("total_voting_num", null == totalVotingNum ? 0 :totalVotingNum);

        return mp;    }

    @Override
    public UserVotes save(UserVotes userVotes) {
        int re = uservotesMapper.insert(userVotes);
        if (re > 0) return userVotes;
        return null;
    }
}
